<%@ page import="java.sql.*" %>
<%--
后端删除处理

问题：
一旦删除用户投过票的选项，这个投票项目就会再次显示到用户界面上，变成没有投过票的项目;
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>删除投票项</title>
</head>
<body>
<%
    int voteId = Integer.parseInt(session.getAttribute("voteId").toString());
    String deleteId = request.getParameter("deleteId");
    try {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai", "root", "123456");
        Statement stmt = connection.createStatement();
        //这里应该是直接用String就可以，JDBC繁琐之处，要注意单双引号
        String sql1 = "delete from vote_option where id =" + deleteId;
        String sql2 = "delete from user_option where option_id =" + deleteId;
        if (deleteId != null) {
            stmt.execute(sql1);
            //user_option中的相关选项也要删除
            stmt.executeUpdate(sql2);
        }
        connection.close();
        stmt.close();
    } catch (Exception e) {
        e.printStackTrace();
        out.write(e.getMessage());
    }
    //带着Vote的id返回上一个也页面
    response.sendRedirect("../adminVoteOption.jsp?id=" + voteId);
%>
</body>
</html>

